'use strict';
const Service = require('../core/base_service.js');
class DeviceService extends Service {


  // brief 
  //deviceImg
  /**
   *  deviceImg: data.img,
   *  brief:data.brief
   * @param {*} data 
   */
  async updateDevice(data) {
    const row = {
      id: data.id,
       
    };
   
    Object.assign(row,data)
    const result = await this.app.mysql.update('Device', row); // 更新 posts 表中的记录
    const updateSuccess = result.affectedRows === 1;
    return updateSuccess;
  }

  async CreateDevice(data){
    console.log("servive",data);
    const row = {
          treeid:data.treeid,
          DeviceName :data.DeviceName,
          parentId:data.parentId,
          endTree:false // 不是最后一个
    };
    //
    
    // Object.assign(row,{
    //   endTree:data.endTree
    // })

    this.ctx.logger.info('DeviceINfo  CreateDevie after assign maybe change endTree QAQ ,on data row',row);
    let result = null ;
    try{
     result = await this.app.mysql.insert('Device',row);
     console.log("result",result);
    }catch(e){
      this.ctx.logger.error(new Error("=========Create Deviec service ======= ",e));
    }
    
    const updateSuccess = result ? result.affectedRows === 1 : false
    this.ctx.logger.info('updateSuccess status',updateSuccess);
    return updateSuccess;
  }
  
  // 获取
  async findDevice(data=null){
    let result = null
    console.log("findDevice",data)
    let where={}
    console.log("data",data.id)
    console.log('tag', isNaN(data.id))
    if(!isNaN(data.id)){
      where={
        where:data
      }
    }
    console.log("where",where)
     try{
        result = await this.app.mysql.select('Device',where);
     }catch(e){
       console.log(e)
     }
     
     console.log("result")
     console.log("getDevice")
     console.log(result)
    //  const updateSuccess = result.affectedRows === 1;
     return result;
   }

  //delete
  async deleteDevice(data){
      let result = null;
      console.log("deleteDevice",data);
      try{
          result = await this.app.mysql.delete('Device', {
            id: data.id,
          });
      }catch(e){
          console.log("====== deleteDevice =====");
          console.log(e);
      }
      return result
  }
}



module.exports = DeviceService;